package sanshi.contect.prototype.v1;

/**
 * clone出来的对象：实际上就是将对象内存空间在复制一份。将复制后地址返回回去
 *因此：在被clone对象的属性中如果存在引用对象的话，克隆出来的对象中的引用对象是一样的。
 * Cloneable：这个中接口是一个标识性接口
 *  这种就是浅克隆
 */
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person(20, 100, new Location("cd", "22"));
        System.out.println(p1);
        Person clonePer = (Person) p1.clone();
        System.out.println(clonePer);
        System.out.println(p1 == clonePer);
        System.out.println(p1.location == clonePer.location);

    }
}
class Person implements Cloneable {

    int age;
    int score;
    Location location;

    public Person(int age, int score, Location location) {
        this.age = age;
        this.score = score;
        this.location = location;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", score=" + score +
                ", location=" + location +
                '}';
    }
}

class Location {
    String locaName;
    String locaYear;

    public Location(String locaName, String locaYear) {
        this.locaName = locaName;
        this.locaYear = locaYear;
    }

    @Override
    public String toString() {
        return "Location{" +
                "locaName='" + locaName + '\'' +
                ", locaYear='" + locaYear + '\'' +
                '}';
    }
}
